package com.bwie.config;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
 * @Auther:${涛}
 * @Date:2024/9/3 20:39
 * @Description: White
 * @Version 1.0.0
 */
//@Configuration
public class White implements GlobalFilter, Ordered {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {


        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String url = request.getPath().toString();

        //获取前端页面头部数据然后判断是否一致
        String[] list = {"/user/login", "/user/code", "/user/judgment","/user/update"};
        for (String s : list) {
            if (url.contains(s)) {
                System.out.println("本次放心的路径URL：" + s);
                return chain.filter(exchange);
            }
        }


        //获取token
        String token = request.getHeaders().getFirst("token");
        //判断token 是否正确
        if (StringUtils.isEmpty(token)) {
            System.out.println("没有获取token");
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }


        Claims claims = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer userId = claims.get("id", Integer.class);

        try {
            //判断token是否无效
            String tokenDb = stringRedisTemplate.opsForValue().get("token:" + userId);
            if (StringUtils.isEmpty(tokenDb)) {
                System.out.println("token无效");
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }

            if (!token.equals(tokenDb)) {
                System.out.println("token异地登录无效");
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
            Long expire = stringRedisTemplate.getExpire("token:" + userId, TimeUnit.MINUTES);
            if (expire < 10) {
                stringRedisTemplate.expire("token:" + userId, 20, TimeUnit.MINUTES);
            }


        } catch (Exception e) {
            System.out.println("非法登录");
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        System.out.println("请求转发url:" + url);
        return chain.filter(exchange);

    }

    @Override
    public int getOrder() {
        return 0;
    }
}


